<%#
 Copyright 2013-2025 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
// ***********************************************************
// This example plugins/index.js can be used to load plugins
//
// You can change the location of this file or turn off loading
// the plugins file with the 'pluginsFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/plugins-guide
// ***********************************************************

// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)
<%_ if (cypressAudit) { _%>
import { existsSync, mkdirSync, writeFileSync } from 'fs';
import { lighthouse, pa11y, prepareAudit } from 'cypress-audit';
<%_ } _%>

export default <% if (cypressCoverage) { %>async <% } %>(on: Cypress.PluginEvents, config: Cypress.PluginConfigOptions) => {
  on('before:browser:launch', (browser, launchOptions) => {
<%_ if (cypressAudit) { _%>
    prepareAudit(launchOptions);
<%_ } _%>
    if (browser.name === 'chrome' && browser.isHeadless) {
      launchOptions.args.push('--disable-gpu');
      return launchOptions;
    }
  });

  // Allows logging with cy.task('log', 'message') or cy.task('table', object)
  on('task', {
    log(message) {
      console.log(message);
      return null;
    },
    table(message) {
      console.table(message);
      return null;
    },
  });

<%_ if (cypressAudit) { _%>
  on('task', {
    lighthouse: lighthouse(async (lighthouseReport) => {
      const { default: ReportGenerator } = await import('lighthouse/report/generator/report-generator');
      if (!existsSync('<%= cypressTemporaryDir %>')) {
        mkdirSync('<%= cypressTemporaryDir %>', { recursive: true });
      }
      writeFileSync('<%= cypressTemporaryDir %>lhreport.html', ReportGenerator.generateReport(lighthouseReport.lhr, 'html'));
    }),
    pa11y: pa11y(),
  });
<%_ } _%>
<%_ if (cypressCoverage) { _%>
  <%_ if (cypressCoverageWebpack) { _%>
  (await import('@cypress/code-coverage/task')).default(on, config);
  <%_ } else { _%>
  if (config.env.CYPRESS_COVERAGE) {
    (await import('cypress-monocart-coverage')).default(on, config, {
      name: 'Cypress Coverage Report',
      entryFilter: {
        '**/node_modules/**': false,
        '**/swagger-ui/**': false,
        '**/polyfills.js': false,
        '**/*.js': true,
      },
      sourceFilter: {
        '**/<%- clientSrcDir %>**': true,
      },
      outputDir: './<%- temporaryDir %>/cypress-coverage-reports',
      reports: ['v8', 'console-details'],
    });
  }
  <%_ } _%>
<%_ } _%>
  return config;
}
